﻿
using System.Globalization;
using System.IO;

namespace GDFlacTool.Common
{
    public class TrackRename
    {
        private readonly string _originalFileName = string.Empty;
        private readonly string _originalPath = string.Empty;
        private readonly string _originalFullPath = string.Empty;
        private string _renamedFileName = string.Empty;
        private string _renamedPath = string.Empty;
        private readonly bool _replaceUnderscores;
        private readonly CaseType _caseType;
        private readonly string _extension = string.Empty;

        public TrackRename(string originalFileName)
        {
            _originalFileName = originalFileName;
        }

        public TrackRename(string originalFileName, string originalPath, string originalFullPath, bool replaceUnderscores, CaseType caseType)
        {
            _originalFileName = originalFileName;
            _originalPath = originalPath;
            _originalFullPath = originalFullPath;
            _extension = string.Format("{0}", Path.GetExtension(_originalFileName));

            _replaceUnderscores = replaceUnderscores;
            _caseType = caseType;
        }

        public string OriginalFileName
        {
            get { return _originalFileName; }
        }

        public string OriginalPath
        {
            get { return _originalPath; }
        }

        public string OriginalFullPath
        {
            get { return _originalFullPath; }
        }

        public string RenamedFileName
        {
            get
            {
                return string.Format("{0}{1}", _renamedFileName, _extension);
            }
            set
            {
                string[] temp = value.Split(Path.DirectorySeparatorChar);
                _renamedFileName = CheckFileName(temp[temp.Length - 1], _replaceUnderscores);
            }
        }

        public string RenamedPath
        {
            get
            {
                if (string.IsNullOrEmpty(_renamedPath))
                {
                    return _originalPath;
                }

                if (Path.IsPathRooted(_renamedPath))
                {
                    return _renamedPath;
                }

                return Path.Combine(_originalPath, _renamedPath);
            }
            set
            {
                if(!string.IsNullOrEmpty(value))
                _renamedPath = CheckPath(value, _replaceUnderscores);
            }
        }

        public string RenamedFullPath
        {
            get
            {
                //if(string.IsNullOrEmpty(_renamedPath))
                //{
                //    return Path.Combine(_originalPath, _renamedFileName);
                //}
                
                //if(Path.IsPathRooted(_renamedPath))
                //{
                //    return Path.Combine(RenamedPath, RenamedFileName);
                //}

                return Path.Combine(RenamedPath, RenamedFileName);
            }
        }

        public string Extension
        {
            get { return _extension; }
        }

        private string CheckFileName(string filename, bool replaceUnderscores)
        {
            string temp = filename;

            temp = Functions.RemoveIllegalCharsFileName(temp);

            temp = temp.Replace("  ", " ");

            if(_caseType != CaseType.None)
            {
                temp = CheckCase(temp, _caseType);
            }

            if(replaceUnderscores)
            {
                temp = temp.Replace(" ", "_");
            }

            return temp;
        }

        private string CheckPath(string path, bool replaceUnderscores)
        {
            string temp = path;

            temp = Functions.RemoveIllegalCharsPath(temp);

            temp = temp.Replace("  ", " ");

            if(replaceUnderscores)
            {
                temp = temp.Replace(" ", "_");
            }

            if (!temp.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
            {
                temp = temp + Path.DirectorySeparatorChar;
            }

            return temp;
        }

        private string CheckCase(string text, CaseType type)
        {
            TextInfo myTI = new CultureInfo("en-US", false).TextInfo;

            string value = text;

            if (type != CaseType.None)
            {
                if (type == CaseType.Lower)
                {
                    value = myTI.ToLower(value);
                }

                if (type == CaseType.Upper)
                {
                    value = myTI.ToUpper(value);
                }

                if (type == CaseType.FirstLetter)
                {
                    value = myTI.ToTitleCase(value.ToLower());
                }

                if (type == CaseType.FirstWord)
                {
                    value = value.ToLower().ToUpperFirstLetter();
                }
            }

            return value;
        }
    }

    //public enum CaseType
    //{
    //    None,
    //    Lower,
    //    Upper,
    //    FirstLetter,
    //    FirstWord
    //}
}
